// 认证相关API
window.authAPI = {
    // 登录接口
    login: async (credentials) => {
        try {
            const response = await apiClient.post('/auth/login', credentials);
            localStorage.setItem('token', response.data.token);
            localStorage.setItem('user', JSON.stringify(response.data.user_info));
            return response;
        } catch (error) {
            console.error('登录请求失败:', error);
            throw error;
        }
    },
    
    // 注册接口
    register: async (userData) => {
        try {
            const response = await apiClient.post('/auth/register', userData);
            return response;
        } catch (error) {
            console.error('注册请求失败:', error);
            throw error;
        }
    },
    
    // 登出接口
    logout: async () => {
        try {
            const response = await apiClient.post('/auth/logout');
            // 清除localStorage中的token和用户信息
            localStorage.removeItem('token');
            localStorage.removeItem('user');
            return response;
        } catch (error) {
            console.error('登出请求失败:', error);
            // 即使请求失败，也清除token
            localStorage.removeItem('token');
            localStorage.removeItem('user');
            throw error;
        }
    },

    // 修改个人信息接口
    updateProfile: async (data) => {
        try {
            const response = await apiClient.put('/auth/profile', data);
            // 更新localStorage中的用户信息
            const currentUser = JSON.parse(localStorage.getItem('user') || '{}');
            const updatedUser = { ...currentUser, ...data };
            localStorage.setItem('user', JSON.stringify(updatedUser));
            return response;
        } catch (error) {
            console.error('修改个人信息失败:', error);
            throw error;
        }
    },

    // 修改密码接口
    changePassword: async (data) => {
        try {
            const response = await apiClient.post('/auth/change-password', data);
            return response;
        } catch (error) {
            console.error('修改密码失败:', error);
            throw error;
        }
    }

};
